package org.example.Controller;

import lombok.extern.slf4j.Slf4j;
import org.example.Pojo.Emp;
import org.example.Pojo.PageResult;
import org.example.Pojo.Result;
import org.example.Server.EmpServer;
import org.example.anno.LogOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
public class EmpController {
    @Autowired
    private EmpServer empServer;

    /*
    * 查询并返回所有的数据
    * */
    @GetMapping("/emps")
    public Result getList(@RequestParam(defaultValue = "1") Integer page ,
                          @RequestParam(defaultValue = "10") Integer pageSize ,
                          String name,
                          Integer gender,
                          @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                          @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end
                          ) {

        log.info("get emps");
        log.info("page : {}", page);
        log.info("pageSize : {}", pageSize);
        log.info("begin : {}", begin);
        log.info("end : {}", end);
        log.info("name : {}", name);
        log.info("gander : {}", gender);
        PageResult pageResult=empServer.getPage(page , pageSize,begin,end,name,gender);
        return Result.success(pageResult);
        //反思：拼写错误导致一系列的错误发生。对时间类的不熟悉！！！！
    }


    /*
    * 添加一个员工(包含多个工作经历)
    * */
    @LogOperation
    @PostMapping("/emps")
    public Result add(@RequestBody Emp emp) {
        log.info("add emp : {}", emp);
        empServer.add(emp);
        return Result.success();
    }



    /*
    * 删除员工通过id数组
    * */
    @LogOperation
    @DeleteMapping("/emps")
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("delete emps : {}", ids);
        empServer.delete(ids);
        return Result.success();
    }

    /*
    * 查询回显
    * */
    @GetMapping("/emps/{id}")
    public Result get(@PathVariable Integer id) {
        log.info("get emp : {}", id);
        Emp emp=empServer.getByID(id);
        return Result.success(emp);
    }


    /*
    * 修改员工的信息
    * */
    @LogOperation
    @PutMapping("/emps")
    public Result update(@RequestBody Emp emp) {
        log.info("update emp : {}", emp);
        empServer.update(emp);
        return Result.success();
    }


    /*
    * 获取全部的员工
    * */
    @GetMapping("/emps/list")
    public Result getAllEmp(){
        log.info("get emps list");
        List<Emp> list=empServer.getAllEmp();
        return Result.success(list);
    }

}
